/*
 * Copyright (c) 2023 iQi.Co.Ltd. All rights reserved.
 */
 
using System.Collections.Generic;
using NetProtocol.POD;
using NetProtocol.Enum;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GamePlay;
using IQIGame.Onigao.Framework;
using Cysharp.Threading.Tasks;
using System;
using static IQIGame.Onigao.Game.MessageWrapper;

namespace NetProtocol.Client {
	/// <summary>
	/// 游戏服gm命令
	/// module : gameCmd
	/// </summary>
	public interface CNetGameCmd : INetClientModule {
		
		public ISession GetSession();
	
		Dictionary<int, INetClientModule.Package2MessageWrapper> INetClientModule.GetModuleFunction()
        {
            MessageFunction execResult_Delegate = (session, wrapper) =>
            {
            	var _wrapper = (MessageWrapper<ExecResultData>)wrapper;
                SC_execResult(session, ref _wrapper.message);
                MessageWrapperPool<ExecResultData>.Put(_wrapper);
            };
        
            Dictionary<int, INetClientModule.Package2MessageWrapper> retDic = new()
            {
                {NetMessageId.gameCmd_execResult, (session, package) => {
                	var result = MessageWrapperPool<ExecResultData>.Get();
                	result.Init(session, execResult_Delegate);
                	result.FromPackage(package);
                	return result;
                }},
            };
            return retDic;
        }
#region Client-side Interface
		/// <summary>
        ///  result可能需要输出
        /// </summary>
		public void SC_execResult(ISession session, ref ExecResultData message) 
		{
            _SC_execResult(session, ref message);
		}
		
		public void _SC_execResult(ISession session, ref ExecResultData message)
		{
#if ENABLE_NET_LOG || UNITY_EDITOR
            NetProtocolHelper.ClientLog(NetMessageId.gameCmd_execResult, "Recv->" + message.ToString());
#endif
			NetProtocolHelper.endCallRequest?.Invoke(NetMessageId.gameCmd_execResult, message.code);
			MsgAsyncAwaiter<ExecResultData>.Instance.CompleteMsgAwait(ref message);
		}
		
#endregion
#region Server-side Interface
		/// <summary>
        ///  执行命令
        /// </summary>
    	public UniTask<ExecResultData> CS_exec(string cmd, string args)
        {
            return _CS_exec(cmd, args);
        }
        
        public UniTask<ExecResultData> _CS_exec(string cmd, string args)
        {
        	ISession session = GetSession();
        	if(session == null) return UniTask.FromResult(default(ExecResultData));
#if ENABLE_NET_LOG || UNITY_EDITOR
			NetProtocolHelper.ClientLog(NetMessageId.gameCmd_exec, "Send->"+"GameCmd.CS_exec { "
				+ "cmd="
				+ cmd
				+ ", args="
				+ args
				+" }");
#endif
        	UniTask<ExecResultData> task = MsgAsyncAwaiter<ExecResultData>.Instance.AddMsgAwait();
        	session.SendPack(Pack_for_exec(cmd, args));
        	NetProtocolHelper.beginCallRequest?.Invoke(NetMessageId.gameCmd_execResult);
			return task;
        }
		
		public static SendPackage Pack_for_exec(string cmd, string args) 
		{
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.gameCmd_exec;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_string(cmd);
			_out.Write_string(args);
			return si_p;
		}
#endregion
#region Messages
        public struct ExecResultData : IReceiveMessage {
        	private int? _code;
        	public int code {get{return _code == null ? -1 : (int)_code;}private set{_code = value;}}
	        public string result;
            public bool IsFail => _code != null && _code != 0;
            public bool IsSuccess =>_code != null && _code == 0;
            public bool IsCanceled => _code == null;
            public void FromPackage(ReceivePackage _package)
            {
                SInputStream _in = _package.InputStream;
                code = _in.HasRemaining() ? _in.Read_int() : default;
                result = _in.HasRemaining() ? _in.Read_string() : default;
            }
            
            public int GetMessageId() {return NetMessageId.gameCmd_execResult;}
            
            public override string ToString()
            {
                return "GameCmd.execResult"+ ", code="+ code+ ", result="+ result;
            }
        }
    }
#endregion
}